<?php
class control_gift extends core_action{
	
	function __construct(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
	}
	
	function index(){
		$model = new core_model_gift();
		$page = intval($_REQUEST['p']) ? intval($_REQUEST['p']) : 1;
		$model->setPage($page);
		$model->setLimit(core_lib_constant::PAGE_NUM);
		$model->setCount(TRUE);
		$rs = $model->select("","","","order by gid desc");
		$params['gift'] = $rs->items;
		$params['pageSize'] = $rs->pageSize;
		$params['page'] = $page;
		$params['gift_cur'] = 'current';
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/gift_list.php', $params);
	}
	
	function show(){
		$gid = intval($_REQUEST['gid']);
		if($gid > 0){
			$srvGift = new core_service_gift($gid);
			$params['gift'] = $srvGift->get();
			$msg = "编辑";
			$m = 'edit';
		}else{
			$srvGift = new core_service_gift();
			$msg = "添加";
			$m = 'create';
		}
		if($_POST){
			$post = $_POST;
			if($post['token'] != md5($_COOKIE['adminKey'])){
				$this->showMsg("非法请求",2);
			}
			if($_FILES['imgFile']['name']){
				$upload = new Qupload();
				$upload->savePath = '../'.core_lib_constant::UPLOAD_PATH.date('Ym/');
				$upload->thumb = true;
				$upload->thumbPrefix = '200_';
				$upload->thumbMaxWidth = '200';
				$upload->thumbMaxHeight = '200';
				$file = $upload->uploadOne($_FILES['imgFile']);
				if($file[0]['savename']) $post['pic_url'] = core_lib_constant::UPLOAD_PATH.date('Ym/').$file[0]['savename'];
			}
			$post['pic_url'] = Qutil::filter($post['pic_url']);
			$post['price'] = Qutil::filter($post['price']);
			$post['gift_name'] = Qutil::filter($post['gift_name']);
			$post['rank'] = intval($post['rank']);
			$post['intro'] = Qutil::filter($post['intro']);
			if($srvGift->$m($post) === false){
				$this->showMsg($msg."出错".$srvGift->getError(),2);
			}
			$model = new core_model_adminlog();
			$model->addLog(4, "{$msg}了礼物{$post['gift_name']}");
			$this->showMsg($msg."成功",1,Qtpl::createUrl('gift', 'index','','admin'));
		}
		$params['gift_cur'] = 'current';
		$params['hname'] = $msg;
		$params['token'] = md5($_COOKIE['adminKey']);
		$this->render('simpla/gift.php', $params);
	}
}